package com.linxz.ui.widgets;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.linxz.ui.R;

/**
 * @author Linxz
 * 创建日期：2019年06月10日 09:25
 * version：v4.0.1
 * 描述：
 */
public class LinxzActionBar extends FrameLayout {
    private Context mContext;
    private LinearLayout layoutActionBar;
    private ImageView imgBack;
    private View viewLine;
    private TextView tvTittle;
    private TextView tvRight;
    private View viewDivider;
    private OnActionBarLeftClickListener leftClickListener;
    private OnActionBarRightClickListener rightClickListener;
    private String tittleStr;
    private String rightStr;
    private boolean showDivider;
    private int bgColor;
    private int tvColor;
    private int backIconRes;
    public LinxzActionBar(@NonNull Context context) {
        super(context);
    }

    public LinxzActionBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
        this.mContext=context;
        init();
        setListener();
    }

    public LinxzActionBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinxzActionBar, defStyleAttr, 0);

        tittleStr = a.getString(R.styleable.LinxzActionBar_titleStr);
        rightStr = a.getString(R.styleable.LinxzActionBar_rightStr);
        showDivider=a.getBoolean(R.styleable.LinxzActionBar_showDivider,false);
        bgColor=a.getColor(R.styleable.LinxzActionBar_actionbar_bg_color,getResources().getColor(R.color.white));
        tvColor=a.getColor(R.styleable.LinxzActionBar_actionbar_tv_color,getResources().getColor(R.color.tv_normal));
        backIconRes=a.getResourceId(R.styleable.LinxzActionBar_actionbar_back_icon, R.drawable.icon_back);
        a.recycle();
    }

    private void init(){
        inflate(mContext, R.layout.ui_inflate_action_bar,this);
        layoutActionBar=findViewById(R.id.layoutActionBarMain);
        imgBack=findViewById(R.id.imgBack);
        viewLine=findViewById(R.id.viewLine);
        tvTittle=findViewById(R.id.tvTittle);
        tvRight=findViewById(R.id.tvRight);
        viewDivider=findViewById(R.id.viewDivider);
        if (!TextUtils.isEmpty(tittleStr)){
            tvTittle.setText(tittleStr);
        }
        if (!TextUtils.isEmpty(rightStr)){
            tvRight.setText(rightStr);
        }
        viewDivider.setVisibility(showDivider ? View.VISIBLE : View.GONE);
        layoutActionBar.setBackgroundColor(bgColor);
        tvTittle.setTextColor(tvColor);
        tvRight.setTextColor(tvColor);
        imgBack.setImageDrawable(getResources().getDrawable(backIconRes));
    }

    private void setListener(){
        imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftClickListener!=null){
                    leftClickListener.onLeftClick();
                }else{
                    Activity activity=getActivity(mContext);
                    if (activity!=null){
                        activity.finish();
                    }
                }
            }
        });
        tvRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rightClickListener!=null){
                    rightClickListener.onRightClick();
                }
            }
        });
    }

    public LinxzActionBar setListener(OnActionBarLeftClickListener leftClickListener){
        this.leftClickListener=leftClickListener;
        return this;
    }

    public LinxzActionBar setListener(OnActionBarRightClickListener rightClickListener){
        this.rightClickListener=rightClickListener;
        return this;
    }

    private Activity getActivity(Context context){
        if (context instanceof Activity){
            return ((Activity) context);
        }else if (context instanceof ContextWrapper){
            ContextWrapper wrapper = (ContextWrapper) context;
            return getActivity(wrapper.getBaseContext());
        }else{
            Toast.makeText(context,"关闭Activity失败",Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    public LinxzActionBar setTittle(String strTittle){
        tvTittle.setText(strTittle);
        return this;
    }

    public LinxzActionBar setViewLinevisible(int visible){
        viewLine.setVisibility(visible);
        return this;
    }

    public LinxzActionBar setLeftIcon(@DrawableRes int drawableLeft){
        imgBack.setImageResource(drawableLeft);
        return this;
    }

    public LinxzActionBar setLeftIcon(Drawable drawableLeft){
        imgBack.setImageDrawable(drawableLeft);
        return this;
    }

    public LinxzActionBar setActionBarColor(int color){
        layoutActionBar.setBackgroundColor(color);
        return this;
    }

    public LinxzActionBar setRightIcon(Drawable drawableRight){
        drawableRight.setBounds( 0, 0, drawableRight.getMinimumWidth(),drawableRight.getMinimumHeight());
        tvRight.setCompoundDrawables(null,null,drawableRight,null);
        tvRight.setVisibility(View.VISIBLE);
        return this;
    }

    public LinxzActionBar setTvRightTextViewColor(int color){
        tvRight.setTextColor(ContextCompat.getColor(getContext(),color));
        return this;
    }

    public LinxzActionBar setTvRightEnabel(boolean enabel){
        if (enabel){
            setTvRightTextViewColor(R.color.tv_normal);
        }else{
            setTvRightTextViewColor(R.color.tv_hint);
        }
        tvRight.setEnabled(enabel);
        return this;
    }

    public interface OnActionBarRightClickListener{
        void onRightClick();
    }

    public interface OnActionBarLeftClickListener{
        void onLeftClick();
    }
}
